package com.higgins.client.knowledge;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.StrategyDTO;
import com.higgins.client.generic.components.GenericTable;
import com.higgins.client.projectmanagement.ProjectItemDTO;

public class KnowledgeTable extends GenericTable implements TreeListener	{
	private static final Logger log = Logger.getLogger(KnowledgeTable.class.getName());
	private final KnowledgeServiceAsync knowledgeService = GWT
			.create(KnowledgeService.class);
	private Long parentId = null;
	private String level = null;

	public KnowledgeTable(KnowledgeColumns columns)	{
		super(columns);
		retrieveDTOs();
	}
	public KnowledgeTable(String level, KnowledgeColumns columns)	{
		super(columns);
		this.level = level;
		retrieveDTOs();
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<KnowledgeItemDTO> knowledgeItemDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (KnowledgeItemDTO knowledgeItemDTO:knowledgeItemDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(knowledgeItemDTO.getId());
			row.add(knowledgeItemDTO.getItem());
			row.add(knowledgeItemDTO.getRelationship());
			row.add(knowledgeItemDTO.getTopLevel());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		KnowledgeItemDTO knowledgeItemDTO = new KnowledgeItemDTO();
		knowledgeItemDTO.setParentId(parentId);
		knowledgeItemDTO.setId(getId(row));
		knowledgeItemDTO.setItem(getText(row, 1));
		knowledgeItemDTO.setRelationship(getBoolean(row, 2));
		knowledgeItemDTO.setTopLevel(getBoolean(row, 3));
		updateRow(knowledgeItemDTO);
	}
	
	protected void retrieveDTOs()	{
		try {
			knowledgeService.retrieveKnowledgeItems(level, parentId, new AsyncCallback<List<KnowledgeItemDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("KnowledgeItemDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<KnowledgeItemDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("KnowledgeItemDTO: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} */
	}	
	private void updateRow(KnowledgeItemDTO knowledgeItemDTO)	{
		try {
			knowledgeService.updateKnowledgeItem(knowledgeItemDTO, new AsyncCallback<Long>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("KnowledgeItemDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(Long result) {
						setRowIdAfterUpdate(result.toString());
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("KnowledgeItemDTO: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}

	@Override
	public void update(List<KnowledgeItemDTO> result) {
		clearTable();
		loadData(convertDTOtoRowArray(result));	
	}
	@Override
	public void setParentId(Long parentId) {
		this.parentId = parentId;
		refresh();
	}
}